/*
 * $Author: cbeams $
 * $Date: 2007-06-04 19:47:27 +0000 (Mon, 04 Jun 2007) $
 * $Rev: 26 $
 */
package com.db4o.eclipse.plugin.model;

import com.db4o.ObjectContainer;
import com.db4o.ObjectSet;

import com.db4o.query.Query;

import com.db4o.reflect.ReflectClass;
import com.db4o.reflect.ReflectField;


public class Db4oDatabase extends TreeParent {
    private ObjectContainer oc;

    public Db4oDatabase(String pathToFile, ObjectContainer oc) {
        super(pathToFile);
        this.oc = oc;
        ReflectClass[] knownClasses = oc.ext().knownClasses();
        for (ReflectClass cls : knownClasses) {
            Db4oStoredClass storedClass = new Db4oStoredClass(this, cls);
            ReflectField[] fields = cls.getDeclaredFields();

            Query query = this.oc.query();
            query.constrain(cls);

            ObjectSet<?> objects = query.execute();

            for (Object object : objects) {
                storedClass.addChild(new Db4oStoredObjectInstance(storedClass, object, fields));
            }

            this.addChild(storedClass);
        }
    }

    public ObjectContainer getObjectContainer() {
        return oc;
    }
}
